package cn.wp.ewallet.common.service.impl;

import cn.wp.common.core.exception.WpException;
import cn.wp.ewallet.common.extension.properties.SmsProperties;
import cn.wp.ewallet.common.service.SmsService;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {



    private DefaultProfile profile = DefaultProfile.getProfile("xxx", "xxx","xxxx");
    private IAcsClient client = new DefaultAcsClient(profile);

    @Override
    public void sendSmsCode(String phone,String mobilePrefix, String smsCode) {
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId","cn-hangzhou");
        if(("+86").equals(mobilePrefix)){
            request.putQueryParameter("SignName", "乐全居");
            request.putQueryParameter("TemplateCode", "SMS_182535435");
            request.putQueryParameter("PhoneNumbers", phone+"");
        }else{
            request.putQueryParameter("SignName", "LeQuanJu");
            request.putQueryParameter("TemplateCode", "SMS_183266392");
            request.putQueryParameter("PhoneNumbers", mobilePrefix.substring(1)+phone);
        }
        request.putQueryParameter("TemplateParam", "{\"code\":\""+smsCode+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("短信发送响应报文：{}",response.getData());
        } catch (ClientException e) {
            log.error("短发接口调用失败,手机号 = {}",phone);
            log.error("短发接口调用失败异常栈",e);
            //throw new WpException("短发接口调用失败，手机号 = "+ phone,e);
        }
    }
}
